﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Module.Entities;
using Framework.Services;
using Framework.Repositories;
using Infrastructure.DataObjects;
using Framework.UnitOfWork;
using Domain.Specification.Users;

namespace Infrastructure.QueryServices
{
    class UserQueryService : ApplicationService, IUserQueryService
    {

        public UserQueryService(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {

        }

        IRepository<User> UserRepository
        {
            get
            {
                return GetRepository<User>();
            }
        }

        public User FindUser(Guid ID)
        {
            return UserRepository.GetByKey(ID);
        }

        public User FindUserByName(string name)
        {
            return UserRepository.Find(new UserNameSpecification(name));
        }

        public object FindUsersByName(string name, string x)
        {
            UserRepository.PageFind(0, 0, new string[] { }, u => true, true);
            var users = UserRepository.FindAll(u => u.Name.Contains(name));
            var userObjects = from u in users
                              select new {
                                ID = u.ID,
                                Name = u.Name
                              };
            return userObjects;
        }

        public IEnumerable<UserObject> FindUsersByName(string name)
        {
            string sqlcmd = string.Format("select Name from Users where Name like '%{0}%'", name);
            return UnitOfWork.SqlQuery<UserObject>(sqlcmd);
        }
    }
}
